package com.app.mingluxing.adapter;

        import android.content.Context;
        import android.graphics.Bitmap;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.BaseAdapter;
        import android.widget.ImageView;
        import android.widget.TextView;

        import com.app.mingluxing.R;
        import com.app.mingluxing.bean.MessageResult;
        import com.app.mingluxing.utils.CommonUtils;
        import com.app.mingluxing.widget.CircleImageView;
        import com.nostra13.universalimageloader.core.DisplayImageOptions;
        import com.nostra13.universalimageloader.core.ImageLoader;
        import com.nostra13.universalimageloader.core.assist.ImageScaleType;
        import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

        import java.util.List;


/**
 * Created by Administrator on 2015/8/11.
 */
        public class MessageItemAdapter extends BaseAdapter {
            private Context ctx;
            private List<MessageResult.MessageData> messageData;
            private DisplayImageOptions options;

            public MessageItemAdapter(Context ctx, List<MessageResult.MessageData> size) {
                this.ctx = ctx;
                this.messageData = size;
                options = new DisplayImageOptions.Builder()
                        .showImageOnLoading(R.drawable.icon_default)
                        .showImageForEmptyUri(R.drawable.icon_default)
                        .showImageOnFail(R.drawable.icon_default)
                        .resetViewBeforeLoading(true)
                        .cacheOnDisk(true).cacheInMemory(true)
                        .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                        .bitmapConfig(Bitmap.Config.RGB_565)
                        .considerExifParams(true)
                        .displayer(new SimpleBitmapDisplayer()).build();
            }

            public void setMessageData(List<MessageResult.MessageData> messageData) {
                this.messageData = messageData;
            }

            @Override
            public int getCount() {
                return messageData.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if(convertView==null){
            convertView=View.inflate(ctx, R.layout.items_news_messages,null);
            viewHolder=new ViewHolder();
            viewHolder.ci_pic=(CircleImageView)convertView.findViewById(R.id.ci_pic);
            viewHolder.tv_title= (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.tv_time= (TextView) convertView.findViewById(R.id.tv_time);
            viewHolder.tv_content= (TextView) convertView.findViewById(R.id.tv_content);
            viewHolder.iv_red_point= (ImageView) convertView.findViewById(R.id.iv_red_point);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        viewHolder.tv_title.setText(CommonUtils.isNull(messageData.get(position).title)?"": messageData.get(position).title);
        viewHolder.tv_time.setText(CommonUtils.isNull(messageData.get(position).sendTime)?"": messageData.get(position).sendTime);
        viewHolder.tv_content.setText(CommonUtils.isNull(messageData.get(position).content) ? "" : messageData.get(position).content);
        ImageLoader.getInstance().displayImage(messageData.get(position).sendPeopleUrl, viewHolder.ci_pic,options);

        if ("0".equals(messageData.get(position).flag)){
            viewHolder.iv_red_point.setVisibility(View.VISIBLE);
        }else {
            viewHolder.iv_red_point.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }
    private class ViewHolder {
        private CircleImageView ci_pic;
        private TextView tv_title;
        private TextView tv_time;
        private ImageView iv_red_point;
        private TextView tv_content;

    }
}
